package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Great;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.app.util.MessageDataUtil;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import cn.wolfcode.trip.base.util.SevenUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IGreatService greatService;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IAiteService aiteService;
    @PostMapping
    public JSONResult regist(User user){
        JSONResult jsonResult = new JSONResult();
        try {
            //用户注册
            //检查账号是否存在,如果存在,提示账号已经存在
            User regist = userService.regist(user);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
            jsonResult.setSuccess(false);
        }
        return jsonResult;
    }
    //编辑个人资料
    @PutMapping
    public JSONResult edit(User user, MultipartFile file){
        JSONResult jsonResult = new JSONResult();
        try {
            //保存照片
            if (file !=null && file.getSize()>0){
                String uploadQiniu = SevenUploadUtil.uploadQiniu(file);
                user.setHeadImgUrl(uploadQiniu);
            }
            //编辑操作
            userService.update(user);
            //编辑成功之后.将数据放入data中回显
           User newUser  = userService.getUser(user.getId());
            jsonResult.setData(newUser);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
            jsonResult.setSuccess(false);
        }
        return jsonResult;
    }


    //设计编辑用户游记的接口
    @GetMapping("/{authorId}/travels")
    public Object getTravel(TravelQueryObject qo){
        return travelService.query(qo);
    }

    //查询此用户的文章总共的点赞数
    @GetMapping("/{authorId}/greatNum")
    public Object getGreatNum(@PathVariable Long authorId){
        return userService.getGreatNum(authorId,Great.NOREAD);
    }

    //查询具体的点赞消息
    @GetMapping("/{authorId}/greatMessage")
    public Object getGreatMessage(@PathVariable Long authorId){
        //获取的点赞消息之后,
        List<Map<String, Object>> list = userService.getGreatMessage(authorId);
        List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : list) {
            Date greatTime = (Date) map.get("greatTime");
            Object time = MessageDataUtil.getData(greatTime);
            System.out.println(map);
            map.put("greatTime",time);
            newList.add(map);
        }
        System.out.println(newList);
        //将点赞消息的状态改成已读状态
        greatService.updateState(authorId,Great.READ);
        return newList;
    }

    //根据用户id查询评论消息
    @GetMapping("/{authorId}/comments")
    public Object getCommentMessage(@PathVariable Long authorId){
        //获取评论消息
        List<Map<String, Object>> list = userService.getCommentMessage(authorId);
        List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : list) {
            Date greatTime = (Date) map.get("updatetime");
            Object time = MessageDataUtil.getData(greatTime);
            map.put("updatetime",time);
            newList.add(map);
        }
        //将点赞消息的状态改成已读状态
        greatService.updateCommentState(authorId,Great.READ);
        return newList;
    }
    //查询评论数
    @GetMapping("/{authorId}/commentNum")
    public Object getCommentNum(@PathVariable Long authorId){
            return userService.getCommentNum(authorId,Great.NOREAD);
    }

    //获取指定id的用户资料
    @GetMapping("/{id}")
    public Object getUser(@PathVariable Long id){
        User user = userService.getUser(id);
        System.out.println(user);
        return user;
    }

    //查询系统通知
    @GetMapping("/{authorId}/systems")
    public Object getSystemMessage(@PathVariable Long authorId){
        //获取评论消息
        List<Map<String, Object>> list = userService.getSyetemMessage(authorId);
        List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : list) {
            Date greatTime = (Date) map.get("creatdate");
            Object time = MessageDataUtil.getData(greatTime);
            map.put("creatdate",time);
            newList.add(map);
        }
        //将消息的状态改成已读状态
        messageService.updateSystemState(authorId,2);
        return newList;
    }
    //查询此用户的系统通知数
    @GetMapping("/{authorId}/systemNum")
    public Object getSystemNum(@PathVariable Long authorId){

        return userService.getSystemNum(authorId,Great.READ);
    }
    //查询此用户的私信数
    @GetMapping("/{authorId}/privateNum")
    public Object getPrivateNum(@PathVariable Long authorId){

        return userService.getPrivateNum(authorId,Great.READ);
    }

    //查询用户的aite条数
    @GetMapping("/{authorId}/aiteNum")
    public Object getaiteNum(@PathVariable Long authorId){
        return userService.getAiteNum(authorId,Great.NOREAD);
    }
   // 查询用户的aite信息
    @GetMapping("/{authorId}/aites")
    public Object getAiteMessage(@PathVariable Long authorId){
        //获取评论消息
        List<Map<String, Object>> list = userService.getAiteMessage(authorId);
        List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : list) {
            Date greatTime = (Date) map.get("createtime");
            Object time = MessageDataUtil.getData(greatTime);
            map.put("createtime",time);
            newList.add(map);
        }
        //将点赞消息的状态改成已读状态
        aiteService.updateAiteState(authorId,Great.READ);
        return newList;
    }
}
